package com.hziee.graduate.ticket.service.impl;

import cn.hutool.db.SqlConnRunner;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hziee.graduate.ticket.dao.OrderDao;
import com.hziee.graduate.ticket.dao.SeatDao;
import com.hziee.graduate.ticket.dao.SeatScheduleDao;
import com.hziee.graduate.ticket.dao.TicketsDao;
import com.hziee.graduate.ticket.dto.R;
import com.hziee.graduate.ticket.entity.Order;
import com.hziee.graduate.ticket.entity.Seat;
import com.hziee.graduate.ticket.entity.SeatSchedule;
import com.hziee.graduate.ticket.entity.Tickets;
import com.hziee.graduate.ticket.service.OrderService;
import com.hziee.graduate.ticket.utils.BeanConvertUtil;
import com.hziee.graduate.ticket.utils.DateUtils;
import com.hziee.graduate.ticket.vo.OrderVO;
import com.hziee.graduate.ticket.vo.TicketVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;


@Service("orderService")
public class OrderServiceImpl extends ServiceImpl<OrderDao, Order> implements OrderService {
   @Resource
   private TicketsDao ticketsDao;

   @Resource
   private SeatScheduleDao seatScheduleDao;

    @Override
    public R<List<OrderVO>> getOrderVO(Long userId) {
        // 获取该用户的所有订单
        List<Order> orderByUser = query().eq("user_id", userId).list();
        List<OrderVO> orderVOByUser = BeanConvertUtil.converList(orderByUser,OrderVO.class);
        // 获取每个订单的
        orderVOByUser.forEach(item -> {
            AtomicReference<Integer> total = new AtomicReference<>(0);
            ArrayList<TicketVO> ticketVOs = new ArrayList<>();
            List<Tickets> ticketByOrder = ticketsDao
                    .selectList(new QueryWrapper<Tickets>()
                            .eq("order_id", item.getOrderId()));
            ticketByOrder.forEach(item2 -> {
                total.updateAndGet(v -> v + item2.getTicketPrice());
                TicketVO ticketVO = ticketsDao.getTicketVO(item2.getTicketId());
                ticketVO.setDuring(DateUtils.formatMinune(ticketVO.getStart(),ticketVO.getEnd()));
                ticketVOs.add(ticketVO);

            });
            item.setTicketVOList(ticketVOs);
            item.setTotal(Integer.parseInt(total.toString()));
        });
        return R.success(orderVOByUser);
    }

    @Override
    @Transactional
    public R<Object> cancelOrder(String orderId) {
        // 删除电影票
        List<Tickets> ticketByOrder = ticketsDao.selectList(new QueryWrapper<Tickets>().eq("order_id", orderId).eq("pay_status",0));
        ticketsDao.delete(new QueryWrapper<Tickets>().eq("order_id", orderId).eq("pay_status",0));
        // 更新座位信息
        ticketByOrder.forEach(item -> {
            SeatSchedule seatSchedule = new SeatSchedule();
            seatSchedule.setSeatStatus(1);
            HashMap<String, Object> queryMap = new HashMap<String, Object>();
            queryMap.put("room_id", item.getRoomId());
            queryMap.put("row_id", item.getSeatRow());
            queryMap.put("col_id", item.getSeatCol());
            UpdateWrapper<SeatSchedule> seatUpdateWrapper = new UpdateWrapper<SeatSchedule>().allEq(queryMap);
            seatScheduleDao.update(seatSchedule, seatUpdateWrapper);
        });
        List<Tickets> ticketByOrder2 = ticketsDao.selectList(new QueryWrapper<Tickets>().eq("order_id", orderId));
        // 删除订单
        if(ticketByOrder2.size() == 0) {
            remove(new QueryWrapper<Order>().eq("order_id", orderId).eq("pay_status",0));
        }
        return R.success(orderId);
    }

    @Override
    @Transactional
    public R<LocalDateTime> payOrder(String orderId) {
        // 更新该订单未支付的电影票
        Tickets tickets = new Tickets();
        tickets.setPayStatus(1);
        ticketsDao.update(tickets,new UpdateWrapper<Tickets>().eq("order_id", orderId).eq("pay_status",0));
        // 更新订单时间
        boolean update = update().eq("order_id", orderId).set("pay_time", LocalDateTime.now()).update();
        return R.success(LocalDateTime.now());
    }

    @Override
    public R<Object> checkReChoose(String orderId) {
        // 检查当前是否可以进行重选

        // 获取订单电影的播放时间

        // 检查电影票是否过了演出时间
        return null;
    }
}

